const Mock = require('mockjs') // 获取mock对象
const Random = Mock.Random // 获取random对象，随机生成各种数据，具体请翻阅文档
const domain = 'http://mockjs.com/api' // 定义默认域名，随便写
const code = 200 // 返回的状态码

// 随机生成文章数据
const postData = req => {
  //   console.log(req) // 请求体，用于获取参数
  let posts = [] // 用于存放文章数据的数组
  for (let i = 0; i < 10; i++) {
    let post = {
      title: Random.csentence(10, 25), // 随机生成长度为10-25的标题
      icon: Random.dataImage('250x250', '文章icon'), // 随机生成大小为250x250的图片链接
      author: Random.cname(), // 随机生成名字
      date: Random.date() + ' ' + Random.time() // 随机生成年月日 + 时间
    }

    posts.push(post)
  }
  // 返回状态码和文章数据posts
  return {
    code,
    posts
  }
}

const login = req => {
  let posts =
  {
    "data": {
      "id": 500,
      "rid": 0,
      "username": "admin",
      "mobile": "123",
      "email": "123@qq.com",
      "token": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjUwMCwicmlkIjowLCJpYXQiOjE1MTI1NDQyOTksImV4cCI6MTUxMjYzMDY5OX0.eGrsrvwHm-tPsO9r_pxHIQ5i5L1kX9RX444uwnRGaIM"
    },
    "meta": {
      "msg": "登录成功",
      "status": 200
    }
  }

  return {
    code,
    posts
  }

}

const userlist = req => {
  let posts =
  {
    "data": {
      "pagenum": 1,
      "total": 32,
    },
    "users": [
      {
        "create_time": 1537243785,
        "email": "admin@aa.com",
        "id": 500,
        "mg_state": true,
        "mobile": "123456789",
        "role_name": "管理员",
        "username": "admin"
      },
      {
        "create_time": 1537243785,
        "email": "admin@aa.com",
        "id": 500,
        "mg_state": true,
        "mobile": "123456789",
        "role_name": "超级管理员",
        "username": "admin"
      },
      {
        "create_time": 1537243785,
        "email": "admin@aa.com",
        "id": 500,
        "mg_state": true,
        "mobile": "123456789",
        "role_name": "超级管理员",
        "username": "admin"
      },
      {
        "create_time": 1537243785,
        "email": "admin@aa.com",
        "id": 500,
        "mg_state": true,
        "mobile": "123456789",
        "role_name": "熊大大",
        "username": "admin"
      },
      {
        "create_time": 1537243785,
        "email": "admin@aa.com",
        "id": 500,
        "mg_state": true,
        "mobile": "123456789",
        "role_name": "员工",
        "username": "admin"
      },
      {
        "create_time": 1537243785,
        "email": "admin@aa.com",
        "id": 500,
        "mg_state": true,
        "mobile": "123456789",
        "role_name": "超级管理员",
        "username": "admin"
      }
    ],
    meta:{
      msg : "获取管理员列表成功",
      status : 200
    }
  }

  return {
    code,
    posts
  }

}

// 定义请求链接，类型，还有返回数据
Mock.mock(`${domain}/posts`, 'get', postData);
Mock.mock(`${domain}/login`, 'get', login);
Mock.mock(`${domain}/getuserslist`, 'get', userlist);


